package level4.display.wrappers
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import level4.assets.AssetsManager;
	
	/**
	 * 数字 包装器
	 * */
	public class NumberWrapper
	{
		/**不足*/
		public static const LACK_NUM:String 		= 	"MoLackNum";
		/**等级*/
		public static const LEVEL_NUM:String 		= 	"Level";
		/**趣币*/
		public static const QUBI_NUM:String 		= 	"MoQuBi";
		/**金币*/
		public static const GAME_NUM:String 		=	"MoGame";
		/**其余,没有 千分位*/
		public static const ITEM_NUM:String			=	"MoProps";
		/**逗号*/
		public static const COMMA:String			=	"Comma";
		/**序号*/
		private const SN:Array = ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"];
		
		public function NumberWrapper()
		{
		}
		/**
		 * 获取 一个数字位图.
		 * @param type : 数字的类型,NumberWrapper枚举
		 * @param value : 数值
		 * @param isThousandsSplit : 是否使用千位分割
		 * @return DisplayObject
		 * 
		 */		
		public function getNumber(type:String,value:int,isThousandsSplit:Boolean=true):DisplayObject{
			
			var numStr:String = value.toString();
			var txtSprite:Sprite = new Sprite(); //数字总容器
			
			var charSprite:DisplayObject;	//单个数字
			var txtArr:Array = [];	//数字sprite数组
			var bitCount:int = 0;	//位数
			//组合数据
			for(var i:int = numStr.length-1;i>=0;i--){
				var index:int = int(numStr.charAt(i));
					charSprite = findDo2d(type+SN[index]);
					charSprite.x = txtSprite.width ;
					bitCount++;
				txtArr.unshift(charSprite);
				//千分位
				if(isThousandsSplit){
					if( bitCount % 3 == 0 && i != 0){ //条件是:能被3整除,又不是最后一个
						charSprite = findDo2d(type + COMMA);
						charSprite.x = txtSprite.width ;
						txtArr.unshift(charSprite);
					}
				}
			}
			//按顺序添加到显示列表
			for(i=0;i<txtArr.length;i++){
				txtArr[i].x = txtSprite.width;
				txtSprite.addChild(txtArr[i]);
			}
			//最后 绘出
			var bmd:BitmapData = new BitmapData(txtSprite.width,txtSprite.height,true,0);
			bmd.draw(txtSprite);
			return new Bitmap(bmd);
		}
		
		/**
		 * 获取 对应的 显示对象
		 * @param className
		 * @return 
		 * 
		 */		
		private function findDo2d(className:String):DisplayObject{
			return new (AssetsManager.getClassFromAllAssets(className))();
		}
	}
}